package com.example.demo_springboot.conf;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//1.maven 添加依赖
//2.添加此处配置bean类
//3.控制器上添加注解,如下:
//@ApiOperation(value="更新信息", notes="根据url的id来指定更新Account信息")
//@ApiImplicitParams({
//        @ApiImplicitParam(name = "id", value = "Account Id", required = true, dataType = "Long",paramType = "path"),
//        @ApiImplicitParam(name = "name", value = "姓名", required = true, dataType = "String",paramType="query"),
//        @ApiImplicitParam(name = "money", value = "money", required = true, dataType = "Double",paramType = "query")
//})

//    重要参数说明:
//    paramType：表示参数放在哪个地方
//    header-->请求参数的获取：@RequestHeader(代码中接收注解)
//    query-->请求参数的获取：@RequestParam(代码中接收注解)
//    path（用于restful接口）-->请求参数的获取：@PathVariable(代码中接收注解)
//    body-->请求参数的获取：@RequestBody(代码中接收注解)
//    form（不常用）

//测试访问: http://localhost:端口号/swagger-ui.html

@Configuration //表明它是一个配置类
@EnableSwagger2 //开启swagger2
public class Swagger2 {

    //配置一些基本的信息。apis()指定扫描的包会生成文档。
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo_springboot.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger构建api文档")
                .description("简单优雅的restfun风格，http://blog.csdn.net/forezp")
                .termsOfServiceUrl("http://blog.csdn.net/forezp")
                .version("1.0")
                .build();
    }
}
